﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.WorkCenters
{
    /// <summary>
    /// 逻辑删除工作中心的资源行信息
    /// </summary>
    public class workCentersWealthDeleteHandler : IRequestHandler<workCentersWealthDeleteCommand, Result>
    {
        private readonly IBaseRepository<WorkCenterWealth> WorkCenterWealthRepository;
        private readonly ILogger<WorkCenterAddHandler> logger;

        public workCentersWealthDeleteHandler(IBaseRepository<WorkCenterWealth> workCenterWealthRepository, ILogger<WorkCenterAddHandler> logger)
        {
            WorkCenterWealthRepository = workCenterWealthRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(workCentersWealthDeleteCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Update };
            try
            {
                var sql = $"UPDATE workCentersWealth SET IsDelWorkCenterWealth = true WHERE WorkCenterWealthId = {request.id};";
                var res = WorkCenterWealthRepository.Update(sql, null);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = null;
                    result.Msg = "删除成功";
                    logger.LogInformation("工作中心资源行信息删除成功");
                }
                else
                {
                    result.Data = null;
                    result.Msg = "删除失败";
                    logger.LogError("工作中心资源行信息删除失败");
                }

            }
            catch (Exception ex)
            {
                logger.LogError($"工作中心资源行信息删除失败," + ex.Message);
                throw;
            }
            return result;
        }
    }
}
